package com.feicheng.iou.controller;


import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.login.UserPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.UserService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/user")
@CrossOrigin
@Slf4j
public class UserController {

    @Autowired
    private UserService userService; //用户业务实现类

    /**
     * @Author wuweinan
     * @Description //添加用户并绑定角色接口
     * @Date 2021/7/28 11:41
     * @Param userPOJO 封装用户和角色集合
     * @return ResultInfo
     */
    @PostMapping
    public ResultInfo addUser(UserPOJO userPOJO){
        log.info("UserController的addUser方法进入，参数addUserPOJO：" + userPOJO);

        //判断请求是否重复
        if (!IdempotentUtils.judge(userPOJO, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"新增用户"));
        }

        //校验参数是否为空
        if (userPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        return userService.AddUser(userPOJO);
    }

    /**
     * @Author wuweinan
     * @Description //根据名字模糊查询用户接口
     * @Date 2021/7/28 11:42
     * @Param account 用户账号
     * @return ResultInfo
     */
    @GetMapping("/all/{account}")
    public ResultInfo queryUserListByCondition(@PathVariable(value = "account") String account){
        log.info("UserController的queryUserListByCondition方法进入,参数account= " + account);

        //校验参数是否为空
        if (account == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        return  userService.queryUserListByCondition(account);
    }

    /**
     * @Author wuweinan
     * @Description //修改用户的信息和角色接口
     * @Date 2021/7/28 13:28
     * @Param addUserPOJO
     * @return ResultInfo
     */
    @PutMapping
    public ResultInfo changeUserMessage(UserPOJO userPOJO){
        log.info("UserController的changeUserMessage方法进入,参数addUserPOJO= " + userPOJO);

        //校验参数是否为空
        if (userPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        return  userService.updateUserMessage(userPOJO);
    }

    /**
     * @Author wuweinan
     * @Description //判断用户名是否存在接口
     * @Date 2021/7/28 13:29
     * @Param account 用户账号
     * @return ResultInfo
     */
    @GetMapping("/duplicate/{account}")
    public ResultInfo duplicateUserAccount(@PathVariable(value = "account") String account) {
        log.info("UserController的judgeUserAccount方法进入，参数为: " + account);

        //校验参数是否为空
        if (account == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        return  userService.judgeUserAccount(account);
    }

    /**
     * @Author wuweinan
     * @Description //查询所有市场管理用户
     * @Date 2021/7/28 13:30
     * @Param
     * @return
     */
    @GetMapping("/market/list")
    public ResultInfo queryAllMarketUser(Integer page,Integer size){
        log.info("UserController的queryAllMarketUser方法进入,page=" + page + ",size=" + size);

        return userService.selectAllMarketUser(null,null);
    }


    /**
     * @Author wuweinan
     * @Description 查询所有用户(包括没有市场权限的)
     * @Date 2021/7/28 13:30
     * @Param
     * @return
     */
    @GetMapping("/list")
    public ResultInfoV110 queryAllUser(Integer page,Integer size){
        log.info("UserController的queryAllUser方法进入,方法没有参数");

        return userService.selectAllUser(page, size);
    }

    /**
     * @Author wuweinan
     * @Description //根据用户Id查询用户接口
     * @Date 2021/7/28 13:33
     * @Param userId 用户id
     * @return  ResultInfo
     */
    @GetMapping("/info/{userId}")
    public ResultInfo queryUserByUserId(@PathVariable (value = "userId") Integer userId){
        log.info("UserController的queryUserByUserId方法进入，参数userId= " + userId);

        //校验参数是否为空
        if (userId == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }
        return userService.selectUserById(userId);
    }
}

